package com.example.education.controller;

import com.example.education.exception.ParamException;
import com.example.education.model.Category;
import com.example.education.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/interface/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/insert")
    public boolean insertCategory(Category category){
        boolean result = false;
        try {
            result = categoryService.insertCategory(category);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/delete")
    public boolean deleteCategory(Integer categoryId){
        boolean result = false;
        try {
            result = categoryService.deleteCategory(categoryId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/update")
    public boolean updateCategory(Category category){
        boolean result = false;
        try {
            result = categoryService.updateCategory(category);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/getById")
    public Category getCategoryById(Integer categoryId){
        Category category = new Category();
        try {
            category = categoryService.getCategoryById(categoryId);
        } catch (ParamException e) {
            e.printStackTrace();
        }
        return category;
    }

    @RequestMapping("/getAll")
    public List<Category> getAll(){
        return categoryService.selectAll();
    }
}
